using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using System.IO;


namespace ERP.View
{
    public partial class viewListHopdong : DevExpress.XtraEditors.XtraForm
    {
        public viewListHopdong()
        {
            InitializeComponent();
        }

        public cs_hmr_loaihopdongInfo loaihd = new cs_hmr_loaihopdongInfo();
        public cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
        public cs_hmr_nhanvienInfo nv = new cs_hmr_nhanvienInfo();
        public cs_hmr_hopdong_nhanvienInfo hdnv = new cs_hmr_hopdong_nhanvienInfo();
        public int flag = 0;
        public int count = 0;
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();

        private void viewListHopdong_Load(object sender, EventArgs e)
        {
            this.hdnv.manv = this.nv.manhanvien;
            if (this.nv.Get_Info() > 0)
            {
                this.Load_Data();
                this.Load_Image();
            }
            else
            {
                this.nbcMenu.Enabled = false;
            }
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    this.btnXoa.Enabled = false;
                }
                if (userright.updatedata == 0)
                {
                    this.btnDieuchinh.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            ds = this.loaihd.Get_List();
            this.cboChucdanh.Properties.DataSource = ds.Tables[0];
            ds = new DataSet("ds");
            ds = this.dv.Get_List_1();
            this.cboDonvi.Properties.DataSource = ds.Tables[0];
            this.txtManv.Text = this.nv.nhanvien_id;
            this.txtHoten.Text = this.nv.hoten;
            ds = new DataSet("ds");
            ds = this.nv.Get_List_Hopdong();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
            }
        }
        private void Load_Image()
        {
            cs_hmr_hinhanh ha = new cs_hmr_hinhanh();
            ha.manhanvien = this.nv.manhanvien;
            if (ha.Get_Info() > 0)
            {
                byte[] arrPicture = ha.hinhanh;
                MemoryStream ms = new MemoryStream(arrPicture);

                peHinhanh.Image = Image.FromStream(ms);
                peHinhanh.SizeMode = PictureBoxSizeMode.StretchImage;
                peHinhanh.BorderStyle = BorderStyle.FixedSingle;

                ms.Close();
            }
        }

        private void Load_Info()
        {
            this.txtNgaybd.Text = this.hdnv.ngaybatdau.ToShortDateString().Trim();
            this.txtNgaykt.Text = this.hdnv.ngayketthuc.ToShortDateString().Trim();
            this.cboChucdanh.EditValue = this.hdnv.maloaihd;
            this.cboDonvi.EditValue = this.hdnv.madv;
            
            this.txtNoidung.Text = this.hdnv.ghichu;
        }
        private void Save_Info()
        {
            this.hdnv.ngaybatdau = Convert.ToDateTime(this.txtNgaybd.Text.Trim());
            this.hdnv.ngayketthuc = Convert.ToDateTime(this.txtNgaykt.Text.Trim());
            this.hdnv.maloaihd = this.cboChucdanh.EditValue.ToString().Trim();
            this.hdnv.madv = this.cboDonvi.EditValue.ToString().Trim();
            //this.hdnv.soqd = this.txtSoqd.Text.Trim();
            this.hdnv.ghichu = this.txtNoidung.Text.Trim();
            if (this.flag == 1)
            {
                this.hdnv.ngaynhap = DateTime.Now.ToShortDateString();
                this.hdnv.nguoinhap = this.user.user_id;
                this.hdnv.ngaysua = DateTime.Now.ToShortDateString();
                this.hdnv.nguoisua = this.user.user_id;
            }
            else
            {
                this.hdnv.ngaysua = DateTime.Now.ToShortDateString();
                this.hdnv.nguoisua = this.user.user_id;
            }
        }

        private void btnMoi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.flag = 1;
            this.hdnv.trangthaihd = 0;
            this.cboDonvi.Focus();
            this.btnLuu.Enabled = true;
            this.btnKhongluu.Enabled = true;
        }

        private void btnDieuchinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.flag = 2;

            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hdnv.manv_hd = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim();
                if (this.hdnv.Get_Info() > 0)
                {
                    this.cboDonvi.Focus();
                    this.btnLuu.Enabled = true;
                    this.btnKhongluu.Enabled = true;
                    this.Load_Info();
                }
            }
        }

        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hdnv.manv_hd = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim();
                if (this.hdnv.Get_Info() > 0)
                {
                    this.hdnv.ExcuteProcedure(3);
                    DataSet ds = new DataSet("ds");
                    ds = this.nv.Get_List_Congtac();
                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        this.count = 0;
                    }
                    else
                    {
                        this.count = ds.Tables[0].Rows.Count;
                        this.grcDanhsach.DataSource = ds.Tables[0];
                    }
                }
            }
        }

        private void btnThoat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.Dispose(true);
        }

        private void btnKhongluu_Click(object sender, EventArgs e)
        {
            this.btnKhongluu.Enabled = false;
            this.btnLuu.Enabled = false;
            this.flag = 0;
        }
    }
}